package com.simple.common.entity.req.vm.pay;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import lombok.Data;

import javax.validation.constraints.Pattern;
import java.util.Map;
import java.util.function.Consumer;

/**
 * 跳转支付
 *
 * @author yasud
 * @date 2020年09月28日 17:58
 * @description
 */
@Data
public class UpopReq extends PayBase {

    /**
     * 跳转地址
     */
    @Pattern(regexp = "^((ht|f)tps?):\\/\\/[\\w\\-]+(\\.[\\w\\-]+)+([\\w\\-\\.,@?^=%&:\\/~\\+#]*[\\w\\-\\@?^=%&\\/~\\+#])?$", message = "redirectUrl format error")
    private String redirectUrl;

    @Override
    public void check(Consumer<ReqBase>... consumers) {
        Preconditions.checkNotNull(getExtraInfo(), "拓展字段不能为空");
        Preconditions.checkState(getAmount().indexOf(".") != -1, "订单金额必须以小数点后两位结束");
        String substring = getAmount().substring(getAmount().indexOf(".") + 1);
        Preconditions.checkState(substring.length() == 2, "订单金额必须以小数点后两位结束");
        super.check();
    }

    @Override
    protected Map<String, Consumer<Object>> predicateMapping() {
        Map<String, Consumer<Object>> of = ImmutableMap.of(
                "productName", o -> {
                    Preconditions.checkNotNull(o, "商品名称不能为空");
                },
                "orderTitle", o -> {
                    Preconditions.checkNotNull(o, "订单标题描述不能为空");
                }
        );
        of = Maps.newLinkedHashMap(of);
        of.putAll(ImmutableMap.of(
                "ipAddress", o -> {
                    Preconditions.checkNotNull(o, "ip地址不能为空");
                }
        ));
        return of;
    }
}
